[contents] [prev] [next] [top] [bottom] (3 out of 5)

How to Control Throwing and Catching

The following sample code illustrates the use of caught and throw again:

guard (
	S1
	guard 
		S2
	catching
		e1: (repair A; caught A)
		e2: (repair B; throw again)
		e3: repair C
	end
) catching
	e4: repair D
	e5: repair E
	all: caught undefined
end
S3

If S2 reports an e1 exception, the following happens:

  1. The system checks if the reported exception is an instance of e1. If so, it executes the expression repair A. The caught statement terminates the search for matching exceptions. Execution exits from the guard expression and continues on to S3. The guard construct returns A.
If S2 reports an e2 exception, the following happens:

  1. The system checks if the exception is an instance of e1, which it is not.
  2. The system checks if the exception is an instance of e2. Since it is, it executes the expression repair B. The throw again expression causes the exception to be thrown again immediately, which means it stops checking catchers in the current catch list and starts checking catchers in the catch list of the surrounding guard expression.
  3. The system checks if the exception is an instance of e4.
  4. The system checks if the exception is an instance of e5.
  5. The catcher all: caught undefined explicitly catches the exception and prevents it from being thrown again.
  6. Execution moves out of the outer guard expression and continues on to expression S3

Reporting Exceptions

To report an exception, call the report function with two arguments:

The actual object to be passed as the second argument varies from exception instance to exception instance.

For example, the format string for the divideByZero exception instance is "Attempt to divide %* by zero" where %* will be replaced by the number on which the attempt to divide by 0 was made. For instance, when an attempt is made to divide 25 by 0, a divideByZero error is reported by the following statement:

report divideByZero 25

The value 25 is passed to the exception's format string and the format string for the reported exception becomes:

"Attempt to divide 25 by zero."

The second argument to report can be a collection if you want to pass more than one object to the format string.

For example, the format string for the noMethod exception requires two input values: the method that was called and the class.

The following statement reports a noMethod exception when an attempt is made to call the nonexistent turnPurple method on an instance of the class Window:

report noMethod #("turnPurple", Window)

When this code is executed, an exception is reported. The formatted result for the exception is:

No "turnPurple" method for Window.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.